package com.jp.knowledge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.model.experience.TiyanApps;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;

import org.xutils.common.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/2/3.
 */

public class ExperienceAllGroupDialog extends Dialog implements RecycleVAdapter.OnItemClickListener {

    private List<GroupModel> groupModels;
    private GroupAdapter groupAdapter;
    private RecyclerView contentRecyclerView;

    public ExperienceAllGroupDialog(Context context) {
        super(context, R.style.custom_idalgo);

        View contentView = View.inflate(context, R.layout.dialog_team_circle_company_select, null);
        ((TextView) contentView.findViewById(R.id.company_selected_title)).setText("请选择要移入的组");
        contentRecyclerView = (RecyclerView) contentView.findViewById(R.id.company_selected_rv);
        contentRecyclerView.setHasFixedSize(true);
        contentRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));

        groupModels = new ArrayList<>();
        groupAdapter = new GroupAdapter(getContext(), new ArrayList<GroupModel>());
        groupAdapter.addItemClickListener(this);
        contentRecyclerView.setAdapter(groupAdapter);

        setContentView(contentView);
        getWindow().getAttributes().height = (int)(DensityUtil.getScreenHeight() * 0.6);
    }

    public void show(List<List<TiyanApps>> datas, List<Integer> groupPosition) {
        contentRecyclerView.smoothScrollToPosition(0);
        groupModels.clear();
        for(int position : groupPosition) {
            GroupModel groupModel = new GroupModel();
            groupModel.setGroupName(datas.get(position).get(0).getGName());
            groupModel.setGroupPosition(position);
            groupModels.add(groupModel);
        }
        groupAdapter.updateAdapter(groupModels);
        super.show();
    }

    @Override
    public void itemSelect(int position) {
        dismiss();
        if(this.groupSelectedListener != null)
            this.groupSelectedListener.onGroupSelected(groupModels.get(position).getGroupPosition());
    }

    private GroupSelectedListener groupSelectedListener;
    public void setGroupSelectedListener(GroupSelectedListener groupSelectedListener){
        this.groupSelectedListener = groupSelectedListener;
    }
    public interface GroupSelectedListener{
        void onGroupSelected(int groupPosition);
    }

    private class GroupAdapter extends RecycleVAdapter<GroupModel> {

        public GroupAdapter(Context context, @NonNull List<GroupModel> list) {
            super(context, list);
        }

        @Override
        public int getLayoutId(int viewType) {
            return R.layout.dialog_team_circle_company_item;
        }

        @Override
        public void onBind(RecycleVHolder holder, int position) {
            holder.getConvertView().setBackgroundResource(R.drawable.normal_click_bg);
            holder.setTextView(R.id.company_name, getData(position).getGroupName());
            ImageView rightIcon = holder.getImageView(R.id.unselect_company);
            rightIcon.setImageResource(R.mipmap.faxiangengduo);
            rightIcon.setClickable(false);
            rightIcon.setEnabled(false);
        }
    }

    private class GroupModel{

        private String groupName;
        private int groupPosition;

        public String getGroupName() {
            return groupName;
        }

        public void setGroupName(String groupName) {
            this.groupName = groupName;
        }

        public int getGroupPosition() {
            return groupPosition;
        }

        public void setGroupPosition(int groupPosition) {
            this.groupPosition = groupPosition;
        }
    }
}
